﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace RM2012Mobile
{
    public partial class frmTables : Form
    {
        public RM2012Service.RM2012Service objService = new RM2012Service.RM2012Service();

        public frmTables()
        {
            InitializeComponent();
        }

        private void frmTables_Load(object sender, EventArgs e)
        {
            lvArea.Items.Clear();
            RM2012Service.AreaInfo[] listArea = objService.GetAllArea();
            if (listArea.Length > 0)
            {
                foreach (RM2012Service.AreaInfo infoArea in listArea)
                {
                    ListViewItem itemArea = new ListViewItem();
                    itemArea.ImageIndex = 0;
                    itemArea.Text = infoArea.AreaName;
                    lvArea.Items.Add(itemArea);
                }
            }

            lvTables.Items.Clear();
            if (listArea.Length > 0)
            {
                string areaId = objService.GetAreaIdByName(lvArea.Items[0].Text);
                RM2012Service.TablesInfo[] list = objService.GetTableByArea(areaId);
                foreach (RM2012Service.TablesInfo info in list)
                {
                    ListViewItem item = new ListViewItem();
                    item.ImageIndex = 1;
                    item.Text = info.TableName;
                    ListViewItem.ListViewSubItem subState = new ListViewItem.ListViewSubItem();
                    if (info.StateId == 1)
                    {
                        subState.Text = "Chưa sử dụng";
                    }
                    else if (info.StateId == 2)
                    {
                        subState.Text = "Đang sử dụng";
                        item.ForeColor = System.Drawing.Color.Red;
                    }
                    else if (info.StateId == 3)
                    {
                        subState.Text = "Đã đặt";
                        item.ForeColor = System.Drawing.Color.Green;
                    }
                    else if (info.StateId == 4)
                    {
                        subState.Text = "Ngừng sử dụng";
                        item.ForeColor = System.Drawing.Color.Gray;
                    }
                    else
                    {
                        subState.Text = "Chưa sử dụng";
                    }
                    item.SubItems.Add(subState);
                    lvTables.Items.Add(item);
                }
            }
        }

        private void lvArea_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {
                int indexSelected = Convert.ToInt32(lvArea.SelectedIndices[0].ToString());
                string areaNameSelected = lvArea.Items[indexSelected].Text;
                string areaIdSelected = objService.GetAreaIdByName(areaNameSelected);

                RM2012Service.TablesInfo[] list = objService.GetTableByArea(areaIdSelected);
                lvTables.Items.Clear();
                foreach (RM2012Service.TablesInfo info in list)
                {
                    ListViewItem item = new ListViewItem();
                    item.ImageIndex = 1;
                    item.Text = info.TableName;
                    ListViewItem.ListViewSubItem subState = new ListViewItem.ListViewSubItem();
                    if (info.StateId == 1)
                    {
                        subState.Text = "Chưa sử dụng";
                    }
                    else if (info.StateId == 2)
                    {
                        subState.Text = "Đang sử dụng";
                        item.ForeColor = System.Drawing.Color.Red;
                    }
                    else if (info.StateId == 3)
                    {
                        subState.Text = "Đã đặt";
                        item.ForeColor = System.Drawing.Color.Green;
                    }
                    else if (info.StateId == 4)
                    {
                        subState.Text = "Ngừng sử dụng";
                        item.ForeColor = System.Drawing.Color.Gray;
                    }
                    else
                    {
                        subState.Text = "Chưa sử dụng";
                    }
                    item.SubItems.Add(subState);
                    lvTables.Items.Add(item);
                }
            }
            catch
            {
            	
            }
        }

        private void menuItemClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void menuItemNotice_Click(object sender, EventArgs e)
        {
            string notice = "";
            notice = notice + "Trạng thái :" + Environment.NewLine;
            notice = notice + "Đỏ - Đang phục vụ" + Environment.NewLine;
            notice = notice + "Xanh lá cây - Đã đặt" + Environment.NewLine;
            notice = notice + "Đen - Bàn trống" + Environment.NewLine;
            notice = notice + "Xám - Bàn không sử dụng" + Environment.NewLine;
            MessageBox.Show(notice);
        }
    }
}